<?php
namespace Home\Controller;
use Think\Controller;
use Home\Model\CartsModel;
use Home\Model\GoodsModel;


class CartsController extends BaseController {


    // 默认 会员ID
    public $member_id;

    public function __construct(){
        parent::__construct();
        $this->member_id = $this->member_data['member_id'];
        if(!$this->member_id){
            //exit(url('Login/index'));
            $this->redirect('Login/index');
        }
    }



    public function addcart(){
        $model = new CartsModel();
        $rs = $model->addCart($this->member_id);
        if($rs['status'] ==1 ){
            $rs['url'] = U('cart');

            $goodsModel = new GoodsModel();
            $all = $goodsModel->getcartnum();
            $pid = (int) I('post.pid');
            $local_id = (int) I('post.local_id');
            $goods_id = (int) I('post.goods_id');
            $p = M('b2c_carts')->where(['member_id'=>$this->member_id,'product_id'=>$pid,'local_id'=>$local_id])->getField('cart_num');
            $rs['product'] = $p ? $p : "0";
            $rs['quanbu'] = $all['num'] ? $all['num'] : "0";
        }

        $this->ajaxReturn($rs);
    }


    //判断库存是否足够
    public function isStore(){

        $local_id = I('post.local_id');
        $pid = (int) I('post.pid');
        $goods_id = (int) I('post.goods_id');
        $spid = (int) I('post.spid');
        $member_id =$this->member_id;
        $cartNum = I('post.buyNum');

        $AddData = array('pid'=>$pid,'local_id'=>$local_id,'goods_id'=>$goods_id,'member_id'=>$member_id,'spid'=>$spid,'cartNum'=>$cartNum);

        //当该用户在购物车，是否有该商品
        $goods = M('b2c_carts')->where(array('member_id'=>$member_id,'product_id'=>$pid,'local_id'=>$local_id))->select();
        $model = new CartsModel();
        //判断库存是否足够
        $res =  $model->apiIsEnough($AddData,$goods[0],$cartNum,$this->member_id);



       // $result = $model->isEnough($pid,$local_id,$this->member_id);

        $this->ajaxReturn($res);
    }


    public function cart(){

        $local_id = I('local_id', '1');

        $storeData = M('webpos_local_store')->where(['local_id'=>$local_id])->find();
        //基于平台商品库 管理查寻商品信息 // p.marketable  上架下架
       $model = new CartsModel();

        //$result =  $model->cartdetail($addR);
       $result =  $model->getCartData($this->member_id);



       $total['name'] = $result['address']['name'];
       $total['area'] = $result['address']['area'];
       $total['addr'] = $result['address']['addr'];
       $total['total_fee'] = $result['total_fee'];
       $total['mobile'] = $result['address']['mobile'];
       $total['goods_total_fee'] = $result['goods_total_fee'];

        if(!empty($result['out_time'])){
            unset($result['out_time']['lp_fee']);
            unset($result['out_time']['shop_checkd']);
            unset($result['out_time']['distance']);
            unset($result['out_time']['shipping']);
            unset($result['out_time']['ship_fee']);
            unset($result['out_time']['total_weight']);
            unset($result['out_time']['store_dot_name']);
            unset($result['out_time']['zT_addr']);
            unset($result['out_time']['expected_time']);
            unset($result['out_time']['send_type']);
            unset($result['out_time']['local_total']);
            unset($result['out_time']['checked_num']);
            unset($result['out_time']['inner_type']);
        }
        $out_time=$result['out_time'];
        unset($result['out_time']);
        unset($result['clearAll']);
        unset($result['tips']);
        unset($result['inner_type']);

        $this->assign('no_goods',$result['no_goods']);
        unset($result['no_goods']);

        if($result['no_address']==1){
            redirect(url('/Address/index'));
        }

        unset($result['no_address']);
        unset($result['is_no_check']);

       $this->assign('out_time',$out_time);
       $this->assign('storeData',$storeData);


        $this->assign('out_time_len',count($out_time));

       $all_check =$result['all_check'];

       unset($result['address']);
        unset($result['store_ok']);
       unset($result['total_fee']);
       unset($result['goods_total_fee']);
       unset($result['all_check']);
       //过期商品
        unset($result['address']);


       $this->assign('all_check',$all_check);
       $this->assign('total',$total);
       $this->assign('cart_detail',$result);
       $this->display("Carts/cart");
    }

    /**
     *更新购物车商品是否为空
     */
    public function isBuyOk(){
        $model = new CartsModel();
        $result =  $model->getCartData($this->member_id);

        if($result['is_no_check']==0){
            $this->ajaxReturn(['url'=>url('Carts/cart'),'status'=>1]);
        }else{
            $this->ajaxReturn(['status'=>0]);
        }

    }





    public function changeShipping(){
        //$_SESSION['member_id']=session('userInfo')['member_id'];
        $data=$_POST;
        $data['member_id']= session('userInfo')['member_id'];
        $model = new CartsModel();
        $result=$model->changeCartShip($data);


        $cartModel=new cartsModel();
        $result2=$cartModel->getCartData($this->member_id);
        $this->ajaxReturn($result2);


    }


    public function changeLocalDot(){
        $data=$_POST;
        $data['member_id']=session('userInfo')['member_id'];
        $data['shipping']=2;
        $where['local_id']=$data['local_id'];
        $where['member_id']=$data['member_id'];
        $result=M('b2c_carts')->where($where)->save($data);
        $this->ajaxReturn($result);
    }

    public function changeCartsNum(){
        //sleep(1);
        $data=$_POST;
        if($data['type']=='plus'){

            $where['id']=$data['cid'];
            $result=M('b2c_carts')->where($where)->find();


            $result['cart_num']++;
            $result1=M('b2c_carts')->where($where)->save($result);
            $cartModel=new cartsModel();
            $result2=$cartModel->getCartData($this->member_id);

            if(!empty($result2['tips'])){
                $result['cart_num']--;
                $result1=M('b2c_carts')->where($where)->save($result);
            }
            //返回购物车数量
            $m = new GoodsModel();
            $car_num =  $m->getcartnum();
            $result2['num'] = $car_num['num'];

            $this->ajaxReturn($result2);
        }else if($data['type']=='minus'){
            if($data['num']<=1){
                return false;
            }
            $where['id']=$data['cid'];
            $result=M('b2c_carts')->where($where)->find();
            if($result['cart_num']<=1){
                return false;
            }
            $result['cart_num']--;
            $result1=M('b2c_carts')->where($where)->save($result);
            $cartModel=new cartsModel();
            $result2=$cartModel->getCartData($this->member_id);

            //返回购物车数量
            $m = new GoodsModel();
            $car_num =  $m->getcartnum();
            $result2['num'] = $car_num['num'];

            $this->ajaxReturn($result2);
        }
        $this->ajaxReturn(false);
    }

    public function changeIsChecked(){
        $data=$_POST;
        if($data['type']=='remove'){
            $where['id']=$data['cid'];
            $result=M('b2c_carts')->where($where)->find();
            if($result['ischeck']==1){
                $result['ischeck']=0;
                $result1=M('b2c_carts')->where($where)->save($result);
                //计算改变返回
                $cartModel=new cartsModel();
                $result2=$cartModel->getCartData($this->member_id);
                $this->ajaxReturn($result2);
            }
        }else if($data['type']=='add'){
            $where['id']=$data['cid'];
            $result=M('b2c_carts')->where($where)->find();
            if($result['ischeck']==0){
                $result['ischeck']=1;
                $result1=M('b2c_carts')->where($where)->save($result);
                //计算改变返回
                $cartModel=new cartsModel();
                $result2=$cartModel->getCartData($this->member_id);
                $this->ajaxReturn($result2);
            }
        }
        $cartModel=new cartsModel();
        $result2=$cartModel->getCartData($this->member_id);
        $this->ajaxReturn($result2);
    }

    public function localChooseAll(){
        $data=$_POST;
        //$data['member_id']=session('userInfo')['member_id'];
        if($data['type']=='remove'){
            //echo 'update sdb_b2c_carts set ischeck=0 where member_id='.$data['member_id'].' and local_id='.$data['lid'].' and ischeck=1';
            $result=M()->query('update sdb_b2c_carts set ischeck=0 where member_id='.session('userInfo')['member_id'].' and local_id='.$data['lid'].' and ischeck=1');
        }else if($data['type']=='add'){
            $result=M()->query('update sdb_b2c_carts set ischeck=1 where member_id='.session('userInfo')['member_id'].' and local_id='.$data['lid'].' and ischeck=0');
        }
        $cartModel=new cartsModel();
        $result2=$cartModel->getCartData($this->member_id);
        $this->ajaxReturn($result2);
    }

    public function isDelete(){
        $data=$_POST;

        M('b2c_carts')->where(['id'=>$data['cid']])->delete();

        $cartModel=new cartsModel();
        $result2=$cartModel->getCartData($this->member_id);
        $this->ajaxReturn($result2);
    }
    public function allChoice(){

        if($_POST['type']=='remove'){
            M('b2c_carts')->where(['member_id'=>session('userInfo')['member_id']])->save(array('ischeck'=>0));
            $cartModel=new cartsModel();
            $result2=$cartModel->getCartData($this->member_id);
            $this->ajaxReturn($result2);
        }elseif ($_POST['type']=='add'){

            M('b2c_carts')->where(['member_id'=>session('userInfo')['member_id']])->save(array('ischeck'=>1));
            $cartModel=new cartsModel();
            $result2=$cartModel->getCartData($this->member_id);
            $this->ajaxReturn($result2);
        }

    }


    /**
     * 清除失效商品
     */
    public function removeOuttimeCart(){
        $data=$_POST;
        //var_dump($data);
        foreach($data['cids'] as $value){
            M('b2c_carts')->where(['id'=>$value])->delete();
        }

        $cartModel=new cartsModel();
        $result2=$cartModel->getCartData($this->member_id);
        $this->ajaxReturn($result2);

    }

    /**
     * 清楚购物车所有商品
     */
    public function clearAll(){
        M('b2c_carts')->where(['member_id'=>session('userInfo')['member_id']])->delete();
        $cartModel=new cartsModel();
        $result2=$cartModel->getCartData($this->member_id);
        $this->ajaxReturn($result2);
    }
    /**
     * 购物车进入下个页面判断库存
     */
    public function storeIsOk(){
        $cartModel=new cartsModel();
        $result2=$cartModel->getCartData($this->member_id);
        $this->ajaxReturn($result2);
    }

    public function uploadIdentity(){
        //上传图片
        $data = $_POST;
        $data['identity_verifi'] = '3';
        $res = M('b2c_members')->where(['member_id'=>session('userInfo')['member_id']])->save($data);
        if($res !== false){
            $this->ajaxReturn(['msg'=>'上传成功!','status'=>1]);
        }else{
            $this->ajaxReturn(['msg'=>'上传失败!','status'=>0]);
        }

    }
   

}